<template>
  <div class="login">
    <div class="boss">
      <img class="img" src="@/assets/logo.png" alt="" />
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="mobile">
          <el-input placeholder="请输入手机号" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input placeholder="请输入验证码" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass"
            >我已阅读并同意[用户协议]</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button class="but" type="primary" @click="But">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { authorizations } from '@/api/login.js'
import { setItem } from '@/utils/auth.js'
export default {
  data () {
    return {
      form: {
        mobile: '13200000000',
        code: '246810',
        isPass: false
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          {
            validator (rule, value, callback) {
              const res = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (res.test(value)) {
                callback()
              } else {
                callback(new Error('您输入的手机号错误'))
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 6,
            message: '请输入六位数的验证码',
            trigger: 'blur'
          }
        ],
        isPass: [
          {
            required: true,
            message: '请勾选',
            trigger: 'change'
          },
          {
            validator (rule, value, callback) {
              if (value) {
                callback()
              } else {
                callback(new Error('请勾选'))
              }
            }
          }
        ]
      }
    }
  },
  methods: {
    But () {
      this.$refs.form.validate(async red => {
        if (red) {
          const res = await authorizations(this.form)
          setItem(res.data.token)
          console.log(res)
          this.$message.success('验证成功')
          this.$router.push('/')
        } else {
          // this.$message.error('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
html,
body {
  width: 100%;
  height: 100%;
  .login {
    width: 100%;
    height: 100%;
    background-image: url(~@/assets/login.png);
    background-size: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .boss {
      width: 460px;
      background-color: #fff;
      padding: 0 35px;
      .img {
        width: 200px;
        display: block;
        margin: 0 auto;
      }
      .but {
        width: 100%;
      }
    }
  }
}
</style>
